package com.jiang.jixiang.ui;

import android.os.Bundle;

import com.jiang.jixiang.R;
import com.jiang.jixiang.api.ApiUrl;
import com.jiang.jixiang.databinding.ActivityAddbankcardBinding;
import com.jiang.jixiang.network.CommonalityModel;
import com.jiang.jixiang.network.NetWorkListener;
import com.jiang.jixiang.network.okHttpModel;
import com.ws.universal.tools.base.BaseActivity;
import com.ws.universal.tools.base.ManageActivity;
import com.ws.universal.tools.eventbus.BaseBusData;
import com.ws.universal.tools.eventbus.BusCode;
import com.ws.universal.tools.eventbus.EventBusUtil;
import com.ws.universal.tools.utils.ToastUtils;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

public class AddBankCardActivity extends BaseActivity implements NetWorkListener {
    private ActivityAddbankcardBinding binding;
    //private Stripe stripe;
    //private CardFormView cardFormView;
    //private CardParams cardParams;
    private String cardNum;
    private int cardMonth;
    private int cardYear;
    private String cardCvc;

    @Override
    protected void initCreate(Bundle savedInstanceState) {
        ManageActivity.putActivity(this.getClass().getSimpleName(), this);
        binding = ActivityAddbankcardBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

    }

    @Override
    protected void initView() {

        binding.btnSubmit.setChangeAlphaWhenPress(true);
        binding.inTitle.titleTextTv.setText(R.string.select_pay_method);
        binding.inTitle.titleLeftBtn.setOnClickListener(view -> {
            finish();
        });

       /* binding.btnSubmit.setOnClickListener(view -> {
            hideSoftInput(binding.btnSubmit);
            if (checkParams()) {
                cardParams = new CardParams(cardNum, cardMonth, cardYear, cardCvc);
                stripeConfig();
                createCardToken(cardParams);
            }
        });*/
    }

    /*private boolean checkParams() {
        cardNum = binding.edCardNum.getText().toString().trim();
        cardMonth = Integer.parseInt(binding.edCardMonth.getText().toString().trim());
        cardYear = Integer.parseInt(binding.edCardYear.getText().toString().trim());
        cardCvc = binding.edCardCvc.getText().toString().trim();
        if (cardNum.length() < 16 || TextUtils.isEmpty(cardNum)) {
            ToastUtils.showShort(getString(R.string.number_length_error));
            return false;
        }
        if (cardMonth == 0 || cardYear == 0 || TextUtils.isEmpty(cardCvc)) {
            ToastUtils.showShort(getString(R.string.content_not_be_empty));
            return false;
        }

        return true;
    }*/


   /* private void createCardToken(CardParams card, String publishableKey) {
        //调用创建token方法
        stripe = new Stripe(getApplicationContext(), publishableKey);
        stripe.createCardToken(card, new ApiResultCallback<Token>() {
            @Override
            public void onSuccess(@NonNull Token token) {
                LogcatUtils.e(" token Success " + token);
                LogcatUtils.e(" token  " + token.getId());

                createCard(token.getId());
            }

            @Override
            public void onError(@NonNull Exception e) {
                LogcatUtils.e(" token Error msg " + e.getMessage());
                LogcatUtils.e(" token Error  " + e.toString());

            }
        });

    }*/

    private void stripeConfig() {
        showProgressDialog(false);
        Map<String, String> params = new HashMap<>();
        okHttpModel.post(ApiUrl.stripeConfig_Api, params, ApiUrl.stripeConfig_Api_ID, this);
    }

    private void createCard(String token) {
        Map<String, String> params = new HashMap<>();
        params.put("token", token);
        okHttpModel.post(ApiUrl.addCard_Api, params, ApiUrl.addCard_Api_ID, this);
    }

    @Override
    protected void FinishDesTroy() {
        ManageActivity.removeActivity(this.getClass().getSimpleName());
    }

    @Override
    public void onSucceed(JSONObject jsonData, int id, CommonalityModel commonality) {
        stopProgressDialog();
        JSONObject data = jsonData.optJSONObject("data");
        switch (id) {
            case ApiUrl.addCard_Api_ID:
                stopProgressDialog();
                ToastUtils.showShort(commonality.getMessage());
                BaseBusData baseBusData = new BaseBusData(BusCode.addCardBuLease);
                EventBusUtil.postEvent(baseBusData);
                finish();
                break;
            case ApiUrl.stripeConfig_Api_ID:
                // createCardToken(cardParams, data.optString("public_key"));
                break;
        }
    }

    @Override
    public void onFail(JSONObject jsonData, int id, CommonalityModel commonality) {
        stopProgressDialog();
        ToastUtils.showShort(commonality.getMessage());
    }

    @Override
    public void onError(int id, Exception e) {
        stopProgressDialog();
        ToastUtils.showShort(e.toString());
    }
}
